<?php

namespace App\Http\Controllers\YApi;

//use Illuminate\Http\Request;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Session;
use Redirect;

use App\Models\Book;
use App\Models\BooksPingfen;

class SetReadStateController extends Controller
{

    public function setreadstate()
    {
		//修改读书的状态 1想读 2在读 3读过
		$bookid = request::input('bookid');
		$uid = request::input('uid');
		$rating = request::input('rating');
		$memo = request::input('memo');
		$read_state = request::input('read_state');

		$bookspingfen = BooksPingfen::where('book_id', $bookid)->where('uid', $uid)->first();
		if($bookspingfen == null)
		{
			// 添加
			$bookspingfen = new Bookspingfen;		
			$bookspingfen->book_id = $bookid;
			$bookspingfen->uid = $uid;
			$bookspingfen->rating = $rating;
			$bookspingfen->private = 0;
			$bookspingfen->read_state = $read_state;
			$bookspingfen->memo = $memo;
			$bookspingfen->add_time = time();
			$bookspingfen->update_time = time();
			$bookspingfen->save();
		}
		else {
			// 更新
			$bookspingfen->book_id = $bookid;
			$bookspingfen->uid = $uid;
			$bookspingfen->rating = $rating;
			$bookspingfen->private = 0;
			$bookspingfen->read_state = $read_state;
			$bookspingfen->memo = $memo;
			$bookspingfen->update_time = time();
			$bookspingfen->save();
		}

		return 1; // 保存成功
    }
    
}